package com.shawbs.vueblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shawbs.vueblog.comom.PageInfo;
import com.shawbs.vueblog.comom.PagePrams;
import com.shawbs.vueblog.comom.Result;
import com.shawbs.vueblog.entity.Blog;
import com.shawbs.vueblog.mapper.BlogMapper;
import com.shawbs.vueblog.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-08-12
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {


//    /**
//     * 分页查询
//     * @param pagePrams
//     * @return
//     */
//    @Override
//    public Result listPage(PagePrams pagePrams){
//        //分页参数
//        Page<Blog> page = new Page<>(pagePrams.getPage(),pagePrams.getPageSize());
//
//        //其它参数
//        LambdaQueryWrapper<Blog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //排序，这里使用的是created字段降序，支持多个条件并列排序
//        lambdaQueryWrapper.orderByDesc(Blog::getCreated);
//        Page<Blog> pageData = (Page<Blog>) super.page(page,lambdaQueryWrapper);
//
//        List<Blog> records = pageData.getRecords();
//        List list = new ArrayList();
//        for (Blog record : records) {
//            list.add(record);
//        }
//        int total = super.count();
//
//        return Result.success(new PageInfo(total, list));
//    }

    @Override
    public Result listPage(int pageNum,int pageSize, String keyWord){
        //分页参数
        Page<Blog> page = new Page<>(pageNum,pageSize);

        //其它参数
        LambdaQueryWrapper<Blog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        lambdaQueryWrapper.like(Blog::getTitle,keyWord);
        lambdaQueryWrapper.like(Blog::getDescription,keyWord);
        //排序，这里使用的是created字段降序，支持多个条件并列排序
        lambdaQueryWrapper.orderByDesc(Blog::getCreated);

        Page<Blog> pageData = (Page<Blog>) super.page(page,lambdaQueryWrapper);

        List<Blog> records = pageData.getRecords();
        List list = new ArrayList();
        for (Blog record : records) {
            list.add(record);
        }
        int total = super.count();

        return Result.success(new PageInfo(total, list));
    }

    public Result getById(Long id) {
        Blog blog = super.getById((id));
        return Result.success(blog);
    }

    @Override
    public Result addBlog(Blog blog) {
        super.save(blog);
        return Result.success("新增成功", null);
    }

    @Override
    public Result editBlog(Blog blog) {
        super.updateById(blog);
        return Result.success("修改成功", null);
    }

    @Override
    public Result removeBlog(Long id) {
        super.removeById(id);
        return Result.success("删除成功", null);
    }

    @Override
    public Result removeBlogs(Collection ids) {
        super.removeByIds(ids);
        return Result.success("删除成功", null);
    }

}
